package com.pshcp.api;

import com.alibaba.fastjson.JSON;
import com.framework.base.ResultBuilder;
import com.framework.base.ResultDTO;
import com.framework.exception.BizException;
import com.framework.logaop.Log;
import com.framework.logaop.LogSourceEnum;
import com.framework.logaop.LogTypeEnum;
import com.pshcp.domain.model.Protocol;
import com.pshcp.service.ProtocolService;
import com.pshcp.vo.protocol.Protocol2UseHelpListVO;
import com.pshcp.vo.protocol.ProtocolConfigVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: TODO
 * @Author: xiaoche
 * @Date: 2022/12/30 18:43
 */
@Api(tags = {"通用接口-协议"})
@Slf4j
@RestController
@RequestMapping("/api/protocol")
public class ProtocolAPI {

    @Autowired
    private ProtocolService protocolService;


    @ApiOperation("获取协议信息-使用帮助")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取协议信息-使用帮助")
    @PostMapping("getProtocol2UseHelp")
    public ResultDTO<List<Protocol2UseHelpListVO>> getProtocol2UseHelp() throws BizException {
        Protocol protocol = protocolService.getProtocolByType("use_help");
        return ResultBuilder.success(protocol == null ? null : JSON.parseArray(protocol.getContent(),Protocol2UseHelpListVO.class));
    }

    @ApiOperation("获取协议信息-法律声明、隐私政策")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取协议信息-法律声明、隐私政策")
    @PostMapping("getProtocol2PrivacyPolicy")
    public ResultDTO<String> getProtocol2PrivacyPolicy() throws BizException {
        Protocol protocol = protocolService.getProtocolByType("privacy_policy");
        return ResultBuilder.success(protocol == null ? null : protocol.getContent());
    }

    @ApiOperation("获取协议信息-用户服务协议")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取协议信息-用户服务协议")
    @PostMapping("getProtocol2ServiceAgreement")
    public ResultDTO<String> getProtocol2ServiceAgreement() throws BizException {
        Protocol protocol = protocolService.getProtocolByType("service_agreement");
        return ResultBuilder.success(protocol == null ? null : protocol.getContent());
    }

    @ApiOperation("获取协议信息-免责声明")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取协议信息-免责声明")
    @PostMapping("getProtocol2Disclaimer")
    public ResultDTO<String> getProtocol2Disclaimer() throws BizException {
        Protocol protocol = protocolService.getProtocolByType("disclaimer");
        return ResultBuilder.success(protocol == null ? null : protocol.getContent());
    }

    @ApiOperation("获取协议信息-热点电话")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取协议信息-热点电话")
    @PostMapping("getProtocol2Hotspot")
    public ResultDTO<String> getProtocol2Hotspot() throws BizException {
        Protocol protocol = protocolService.getProtocolByType("hotspot");
        return ResultBuilder.success(protocol == null ? null : protocol.getContent());
    }

    @ApiOperation("获取配置信息")
    @Log(source = LogSourceEnum.APP, type = LogTypeEnum.SELECT, desc = "获取配置信息")
    @PostMapping("getProtocolConfig")
    public ResultDTO<List<ProtocolConfigVO>> getProtocolConfig() throws BizException {
        List<ProtocolConfigVO> configVOS = new ArrayList<>();
        Protocol protocol = protocolService.getProtocolByType("medical_guide");
        Protocol protocol1 = protocolService.getProtocolByType("out_info");
        Protocol protocol2 = protocolService.getProtocolByType("popular_science");
        Protocol protocol3 = protocolService.getProtocolByType("book_notice");
        Protocol protocol4 = protocolService.getProtocolByType("register_notice");
        Protocol protocol5 = protocolService.getProtocolByType("book_notice_content");
        Protocol protocol6 = protocolService.getProtocolByType("medical_add_content");
        Protocol protocol7 = protocolService.getProtocolByType("online_service");
        Protocol protocol8 = protocolService.getProtocolByType("informed_consent");
        Protocol protocol9 = protocolService.getProtocolByType("service_notice");
        configVOS.add(build2ConfigVO(protocol));
        configVOS.add(build2ConfigVO(protocol1));
        configVOS.add(build2ConfigVO(protocol2));
        configVOS.add(build2ConfigVO(protocol3));
        configVOS.add(build2ConfigVO(protocol4));
        configVOS.add(build2ConfigVO(protocol5));
        configVOS.add(build2ConfigVO(protocol6));
        configVOS.add(build2ConfigVO(protocol7));
        configVOS.add(build2ConfigVO(protocol8));
        configVOS.add(build2ConfigVO(protocol9));
        return ResultBuilder.success(configVOS);
    }

    private ProtocolConfigVO build2ConfigVO(Protocol protocol){
        ProtocolConfigVO configVO = new ProtocolConfigVO();
        configVO.setCode(protocol.getType());
        configVO.setValue(protocol.getContent());
        configVO.setRemark(protocol.getRemark());
        return configVO;
    }


}
